package com.manelrodriguez.applications.eurostoxx50.to;

import com.manelrodriguez.applications.eurostoxx50.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Parcel;
import android.os.Parcelable;

public final class Empresa implements Parcelable {
	public static final String PREFS_NAME = "MyPrefs";
	public static final String EMPRESA = "EMPRESA";
	public static final String FAVORITES_IDS = "IDS_FAVORITOS";
	public static final String FAVORITES_EMPRESAS_IDS = "IDS_EMPRESA_FAVORITOS";
	
	private String StockSymbol;
	private String LastTradeAmount;
	private String LastTradeDateTime;
	private String StockChange;
	private String OpenAmount;
	private String DayHigh;
	private String DayLow;
	private String StockVolume;
	private String PrevCls;
	private String ChangePercent;
	private String FiftyTwoWeekRange;
	private String EarnPerShare;
	private String PE;
	private String CompanyName;
	private String QuoteError;
	
	public Empresa(){
		super();
	}
	public String getStockSymbol() {
		return StockSymbol;
	}
	public void setStockSymbol(String stockSymbol) {
		StockSymbol = stockSymbol;
	}
	public String getLastTradeAmount() {
		return LastTradeAmount;
	}
	public void setLastTradeAmount(String lastTradeAmount) {
		LastTradeAmount = lastTradeAmount;
	}
	public String getLastTradeDateTime() {
		return LastTradeDateTime;
	}
	public void setLastTradeDateTime(String lastTradeDateTime) {
		LastTradeDateTime = lastTradeDateTime;
	}
	public String getStockChange() {
		return StockChange;
	}
	public void setStockChange(String stockChange) {
		StockChange = stockChange;
	}
	public String getOpenAmount() {
		return OpenAmount;
	}
	public void setOpenAmount(String openAmount) {
		OpenAmount = openAmount;
	}
	public String getDayHigh() {
		return DayHigh;
	}
	public void setDayHigh(String dayHigh) {
		DayHigh = dayHigh;
	}
	public String getDayLow() {
		return DayLow;
	}
	public void setDayLow(String dayLow) {
		DayLow = dayLow;
	}
	public String getStockVolume() {
		return StockVolume;
	}
	public void setStockVolume(String stockVolume) {
		StockVolume = stockVolume;
	}
	public String getPrevCls() {
		return PrevCls;
	}
	public void setPrevCls(String prevCls) {
		PrevCls = prevCls;
	}
	public String getChangePercent() {
		return ChangePercent;
	}
	public void setChangePercent(String changePercent) {
		ChangePercent = changePercent;
	}
	public String getFiftyTwoWeekRange() {
		return FiftyTwoWeekRange;
	}
	public void setFiftyTwoWeekRange(String fiftyTwoWeekRange) {
		FiftyTwoWeekRange = fiftyTwoWeekRange;
	}
	public String getEarnPerShare() {
		return EarnPerShare;
	}
	public void setEarnPerShare(String earnPerShare) {
		EarnPerShare = earnPerShare;
	}
	public String getPE() {
		return PE;
	}
	public void setPE(String pE) {
		PE = pE;
	}
	public String getCompanyName() {
		return CompanyName;
	}
	public void setCompanyName(String companyName) {
		CompanyName = companyName;
	}
	public String getQuoteError() {
		return QuoteError;
	}
	public void setQuoteError(String quoteError) {
		QuoteError = quoteError;
	}
	
	public int describeContents() {

		return 0;
	}
	public void writeToParcel(Parcel out, int flags) {
		out.writeString(StockSymbol);
		out.writeString(LastTradeAmount);
		out.writeString(LastTradeDateTime);
		out.writeString(StockChange);
		out.writeString(OpenAmount);
		out.writeString(DayHigh);
		out.writeString(DayLow);
		out.writeString(StockVolume);
		out.writeString(PrevCls);
		out.writeString(ChangePercent);
		out.writeString(FiftyTwoWeekRange);
		out.writeString(EarnPerShare);
		out.writeString(PE);
		out.writeString(CompanyName);
		out.writeString(QuoteError);
	}
	
	// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<Empresa> CREATOR = new Parcelable.Creator<Empresa>() {
        public Empresa createFromParcel(Parcel in) {
            return new Empresa(in);
        }

        public Empresa[] newArray(int size) {
            return new Empresa[size];
        }
    };

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private Empresa(Parcel in) {
    	StockSymbol = in.readString();
    	LastTradeAmount = in.readString();
    	LastTradeDateTime = in.readString();
    	StockChange = in.readString();
    	OpenAmount = in.readString();
    	DayHigh = in.readString();
    	DayLow = in.readString();
    	StockVolume = in.readString();
    	PrevCls = in.readString();
    	ChangePercent = in.readString();
    	FiftyTwoWeekRange = in.readString();
    	EarnPerShare = in.readString();
    	PE = in.readString();
    	CompanyName = in.readString();
    	QuoteError = in.readString();
    }

    public boolean addFavorite(int posicion, Context context){
    	boolean add = true;
    	SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        String idFavoritos = settings.getString(FAVORITES_IDS, "");
        String idEmpresasFavoritas = settings.getString(FAVORITES_EMPRESAS_IDS, "");
        if(idFavoritos.equals("")){
        	idFavoritos = String.valueOf(posicion);
        	idEmpresasFavoritas = StockSymbol;
        }else{
        	for(String idEmpresa:idFavoritos.split("#")){
        		int intIdEmpresa = Integer.valueOf(idEmpresa);
        		if(intIdEmpresa == posicion)
        		{
        			add = false;
        		}
        	}
        	idFavoritos = idFavoritos + "#" + String.valueOf(posicion);
        	idEmpresasFavoritas = idEmpresasFavoritas + "#" + StockSymbol;
        }
        if(add){
        	editor.putString(FAVORITES_IDS, idFavoritos);
        	editor.putString(FAVORITES_EMPRESAS_IDS, idEmpresasFavoritas);
        	editor.commit();
        	return true;
        }else{
        	return false;
        }
    }
    
    public void borrarFavorito(int posicion, Context context){
    	SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		String idFavoritos = settings.getString(FAVORITES_IDS, "");
		String idEmpresaFavorito = settings.getString(FAVORITES_EMPRESAS_IDS, "");
		String aux = "";
		String aux2 = "";
		if(idFavoritos!= null && !idFavoritos.equals("")){
			String[] ids = idFavoritos.split("#");
	    	for(int i = 0;i<ids.length;i++){
	    		if(i != posicion){
	    			if(aux.equals("")){
	    				aux = ids[i];
	    			}else{
	    				aux = aux + "#" + ids[i];
	    			}
	    		}
	    	}
	    }
		if(idEmpresaFavorito!= null && !idEmpresaFavorito.equals("")){
			String[] idsempresas = idFavoritos.split("#");
	    	for(int i = 0;i<idsempresas.length;i++){
	    		if(StockSymbol != idsempresas[i]){
	    			if(aux2.equals("")){
	    				aux2 = idsempresas[i];
	    			}else{
	    				aux2 = aux2 + "#" + idsempresas[i];
	    			}
	    		}
	    	}
	    }
		editor.putString(FAVORITES_IDS, aux);
		editor.putString(FAVORITES_EMPRESAS_IDS, aux2);
        editor.commit();
	}
    
    public boolean isFavorite(Context context){
    	boolean isFavorito = false;
    	
    	SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    	String idEmpresasFavoritas = settings.getString(FAVORITES_IDS, "");
    	if(!idEmpresasFavoritas.equals("")){
    		for(String idEmpresa:idEmpresasFavoritas.split("#")){
        		if(idEmpresa.equals(StockSymbol))
        		{
        			isFavorito = true;
        		}
        	}
        }
    	
    	return isFavorito;
    }

    public static String getCodeString(String empresa){
    	if(empresa.equals("Eurostoxx50")) return "EUROSTOXX50";
    	else if(empresa.equals("ANHEUS-BUSCH INBEV")) return "ABI.BR";
    	else if(empresa.equals("Air Liquide")) return "AI.PA";
    	else if(empresa.equals("Allianz")) return "ALV.DE";
    	else if(empresa.equals("ASML Holding")) return "ASML.AS";
    	else if(empresa.equals("Basf")) return "BAS.DE";
    	else if(empresa.equals("Bayer")) return "BAYN.DE";
    	else if(empresa.equals("BBVA")) return "BBVA.MC";
    	else if(empresa.equals("BMW")) return "BMW.DE";
    	else if(empresa.equals("Danone")) return "BN.PA";
    	else if(empresa.equals("BNP Pariba")) return "BNP.PA";
    	else if(empresa.equals("Carrefour")) return "CA.PA";
    	else if(empresa.equals("CRH PLC")) return "CRG.IR";
    	else if(empresa.equals("AXA Group")) return "CS.PA";
    	else if(empresa.equals("Daimler AG")) return "DAI.DE";
    	else if(empresa.equals("Deutsche Bank AG")) return "DBK.DE";
    	else if(empresa.equals("Vinci")) return "DG.PA";
    	else if(empresa.equals("Deutsche Telekom")) return "DTE.DE";
    	else if(empresa.equals("Essilor International")) return "EI.PA";
    	else if(empresa.equals("Enel")) return "ENEL.MI";
    	else if(empresa.equals("Eni")) return "ENI.MI";
    	else if(empresa.equals("E.ON")) return "EOAN.DE";
    	else if(empresa.equals("Total")) return "FP.PA";
    	else if(empresa.equals("France Telecom")) return "FTE.PA";
    	else if(empresa.equals("Assicurazioni Generali")) return "G.MI";
    	else if(empresa.equals("Societe Generale Group")) return "GLE.PA";
    	else if(empresa.equals("GDF Suez")) return "GSZ.PA";
    	else if(empresa.equals("Iberdrola")) return "IBE.MC";
    	else if(empresa.equals("ING Groep")) return "INGA.AS";
    	else if(empresa.equals("Intesa Sanpaolo")) return "ISP.MI";
    	else if(empresa.equals("Inditex")) return "ITX.MC";
    	else if(empresa.equals("LVMH")) return "MC.PA";
    	else if(empresa.equals("Munich")) return "MUV2.DE";
    	else if(empresa.equals("L\'Oreal")) return "OR.PA";
    	else if(empresa.equals("Philips electr")) return "PHIA.AS";
    	else if(empresa.equals("Repsol")) return "REP.MC";
    	else if(empresa.equals("RWE")) return "RWE.DE";
    	else if(empresa.equals("Banco Santander")) return "SAN.MC";
    	else if(empresa.equals("Sanofi")) return "SAN.PA";
    	else if(empresa.equals("SAP")) return "SAP.DE";
    	else if(empresa.equals("Compagnie de Saint-Gobain")) return "SGO.PA";
    	else if(empresa.equals("Siemens")) return "SIE.DE";
    	else if(empresa.equals("Schneider Electric")) return "SU.PA";
    	else if(empresa.equals("Telefonica")) return "TEF.MC";
    	else if(empresa.equals("UniCredit")) return "UCG.MI";
    	else if(empresa.equals("Unibail-Rodamco")) return "UL.PA";
    	else if(empresa.equals("Unilever")) return "UNA.AS";
    	else if(empresa.equals("Vivendi")) return "VIV.PA";
    	else if(empresa.equals("Volkswagen")) return "VOW3.DE";

		else return "";
    }
    
    public static String getIdEmpresaString(String companyName, Context context){
    	if(companyName.equals("^STOXX50E")) return context.getString(R.string.EUROSTOXX50);
    	else if(companyName.equals("ABI.BR")) return context.getString(R.string.ABI_BR);
		else if(companyName.equals("AI.PA")) return context.getString(R.string.AI_PA);
		else if(companyName.equals("ALV.DE")) return context.getString(R.string.ALV_DE);
		else if(companyName.equals("ASML.AS")) return context.getString(R.string.ASML_AS);
		else if(companyName.equals("BAS.DE")) return context.getString(R.string.BAS_DE);
		else if(companyName.equals("BAYN.DE")) return context.getString(R.string.BAYN_DE);
		else if(companyName.equals("BBVA.MC")) return context.getString(R.string.BBVA_MC);
		else if(companyName.equals("BMW.DE")) return context.getString(R.string.BMW_DE);
		else if(companyName.equals("BN.PA")) return context.getString(R.string.BN_PA);
		else if(companyName.equals("BNP.PA")) return context.getString(R.string.BNP_PA);
		else if(companyName.equals("CA.PA")) return context.getString(R.string.CA_PA);
		else if(companyName.equals("CRG.IR")) return context.getString(R.string.CRG_IR);
		else if(companyName.equals("CS.PA")) return context.getString(R.string.CS_PA);
		else if(companyName.equals("DAI.DE")) return context.getString(R.string.DAI_DE);
		else if(companyName.equals("DBK.DE")) return context.getString(R.string.DBK_DE);
		else if(companyName.equals("DG.PA")) return context.getString(R.string.DG_PA);
		else if(companyName.equals("DTE.DE")) return context.getString(R.string.DTE_DE);
		else if(companyName.equals("EI.PA")) return context.getString(R.string.EI_PA);
		else if(companyName.equals("ENEL.MI")) return context.getString(R.string.ENEL_MI);
		else if(companyName.equals("ENI.MI")) return context.getString(R.string.ENI_MI);
		else if(companyName.equals("EOAN.DE")) return context.getString(R.string.EOAN_DE);
		else if(companyName.equals("FP.PA")) return context.getString(R.string.FP_PA);
		else if(companyName.equals("FTE.PA")) return context.getString(R.string.FTE_PA);
		else if(companyName.equals("G.MI")) return context.getString(R.string.G_MI);
		else if(companyName.equals("GLE.PA")) return context.getString(R.string.GLE_PA);
		else if(companyName.equals("GSZ.PA")) return context.getString(R.string.GSZ_PA);
		else if(companyName.equals("IBE.MC")) return context.getString(R.string.IBE_MC);
		else if(companyName.equals("INGA.AS")) return context.getString(R.string.INGA_AS);
		else if(companyName.equals("ISP.MI")) return context.getString(R.string.ISP_MI);
		else if(companyName.equals("ITX.MC")) return context.getString(R.string.ITX_MC);
		else if(companyName.equals("MC.PA")) return context.getString(R.string.MC_PA);
		else if(companyName.equals("MUV2.DE")) return context.getString(R.string.MUV2_DE);
		else if(companyName.equals("OR.PA")) return context.getString(R.string.OR_PA);
		else if(companyName.equals("PHIA.AS")) return context.getString(R.string.PHIA_AS);
		else if(companyName.equals("REP.MC")) return context.getString(R.string.REP_MC);
		else if(companyName.equals("RWE.DE")) return context.getString(R.string.RWE_DE);
		else if(companyName.equals("SAN.MC")) return context.getString(R.string.SAN_MC);
		else if(companyName.equals("SAN.PA")) return context.getString(R.string.SAN_PA);
		else if(companyName.equals("SAP.DE")) return context.getString(R.string.SAP_DE);
		else if(companyName.equals("SGO.PA")) return context.getString(R.string.SGO_PA);
		else if(companyName.equals("SIE.DE")) return context.getString(R.string.SIE_DE);
		else if(companyName.equals("SU.PA")) return context.getString(R.string.SU_PA);
		else if(companyName.equals("TEF.MC")) return context.getString(R.string.TEF_MC);
		else if(companyName.equals("UCG.MI")) return context.getString(R.string.UCG_MI);
		else if(companyName.equals("UL.PA")) return context.getString(R.string.UL_PA);
		else if(companyName.equals("UNA.AS")) return context.getString(R.string.UNA_AS);
		else if(companyName.equals("VIV.PA")) return context.getString(R.string.VIV_PA);
		else if(companyName.equals("VOW3.DE")) return context.getString(R.string.VOW3_DE);
		else return companyName;
    }
}
